import shopService from 'services/shopService.js'
import productService from 'services/productService.js'
import * as notifyUtil from 'utils/notifyUtil.js'

export default {
  data() {
    return {}
  },
  methods: {
    getShopById(products, index) {
      if (index < products.length) {
        let shopId = products[index].shopid
        shopService
          .getShopById(shopId)
          .then(response => {
            if (
              response.data.Shop === null ||
              response.data.Shop === undefined
            ) {
              notifyUtil.showError('查找商店失败', '服务器错误')
            } else {
              this.shops.push(response.data.Shop[0])
              this.getShopById(products, ++index)
            }
          })
          .catch(error => {
            console.log(error)
            this.getShopById(products, ++index)
          })
      } else {
        this.gotoStores()
      }
    },
    getShops() {
      shopService.getShopsByName(this.keyword).then(response => {
        if (response.data.Shop === null || response.data.Shop === undefined) {
          // notifyUtil.showError('查找商店失败', '暂无相关商店')
          // this.getShopsByProductName()
        } else {
          let responseShops = response.data.Shop
          this.$set(this, 'shops', responseShops)
        }
        this.getShopsByProductName()
      })
    },
    getShopsByProductName() {
      productService.getProductsByName(this.keyword).then(response => {
        if (
          response.data.Product === null ||
          response.data.Product === undefined
        ) {
          // notifyUtil.showError('查找商品失败', '暂无相关商品')
          this.gotoStores()
        } else {
          this.getShopById(response.data.Product, 0)
        }
      })
    },
    searchShops() {
      this.getShops()
    },
    gotoStores() {
    //   console.log(this.shops)
    }
  }
}
